package poradnik.medyczny.Model;

import java.util.ArrayList;

import poradnik.medyczny.DB.DBAdapter;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

public class Advices {
	long id = 0;
	long disease_id;
	long time;
	boolean if_success;
	boolean if_touched;

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public long getDisease_id() {
		return disease_id;
	}

	public void setDisease_id(long disease_id) {
		this.disease_id = disease_id;
	}

	public long getTime() {
		return time;
	}

	public void setTime(long time) {
		this.time = time;
	}

	public boolean isIf_success() {
		return if_success;
	}

	public void setIf_success(boolean if_success) {
		this.if_success = if_success;
	}

	public boolean isIf_touched() {
		return if_touched;
	}

	public void setIf_touched(boolean if_touched) {
		this.if_touched = if_touched;
	}

	public boolean Save(Context context) {
		DBAdapter medical_db = new DBAdapter(context);
		medical_db.openDataBase();

		ContentValues values = new ContentValues();
		values.put(DBAdapter.ADVICES_TIME, time);
		values.put(DBAdapter.ADVICES_DISEASE_ID, disease_id);
		values.put(DBAdapter.ADVICES_IF_SUCCESS, if_success ? 0 : 1);
		values.put(DBAdapter.ADVICES_IF_TOUCHED, if_touched ? 0 : 1);
		boolean successful = false;
		if (id != 0)
			successful = medical_db.update(DBAdapter.TABLE_ADVICES, values, DBAdapter.ADVICES_ID + "='" + id + "'", null);
		if (!successful) {
			id = medical_db.insert(DBAdapter.TABLE_ADVICES, values);
			successful = id == 0 ? false : true;
		}
		medical_db.close();

		return successful;
	}

	public Advices(long id, Context context) {
		DBAdapter medical_db = new DBAdapter(context);
		medical_db.openDataBase();
		Cursor cursor = medical_db.getAdvice(id);
		if (cursor != null)
			if (cursor.moveToFirst()) {
				this.id = cursor.getLong(cursor.getColumnIndex(DBAdapter.ADVICES_ID));
				time = cursor.getLong(cursor.getColumnIndex(DBAdapter.ADVICES_TIME));
				disease_id = cursor.getLong(cursor.getColumnIndex(DBAdapter.ADVICES_DISEASE_ID));
				if_success = cursor.getInt(cursor.getColumnIndex(DBAdapter.ADVICES_IF_SUCCESS)) == 0;
				if_touched = cursor.getInt(cursor.getColumnIndex(DBAdapter.ADVICES_IF_TOUCHED)) == 0;

			}
		medical_db.close();
	}

	public Advices() {
	}

	public static ArrayList<Advices> getNotSentAdvices(Context context) {
		ArrayList<Advices> list = new ArrayList<Advices>();
		DBAdapter medical_db = new DBAdapter(context);
		medical_db.openDataBase();
		Cursor cursor = medical_db.getNotSentAdvices();
		if (cursor != null) {
			while (cursor.moveToNext()) {
				Advices loc = new Advices();
				loc.id = cursor.getLong(cursor.getColumnIndex(DBAdapter.ADVICES_ID));
				loc.disease_id = cursor.getLong(cursor.getColumnIndex(DBAdapter.ADVICES_DISEASE_ID));
				loc.time = cursor.getLong(cursor.getColumnIndex(DBAdapter.ADVICES_TIME));
				loc.if_success = cursor.getInt(cursor.getColumnIndex(DBAdapter.ADVICES_IF_SUCCESS)) == 0;
				loc.if_touched = cursor.getInt(cursor.getColumnIndex(DBAdapter.ADVICES_IF_TOUCHED)) == 0;
				list.add(loc);
			}
			cursor.close();
		}
		medical_db.close();
		return list;
	}
}
